//
//  MainMHViewController.m
//  project
//
//  Created by 张振宇 on 15/11/5.
//  Copyright © 2015年 lanou. All rights reserved.
//

#import "MainMHViewController.h"
#import "PKRequestManager.h"
#import "OtherTableViewModel.h"
#import "OtherTableViewCell.h"
#import "StrategyDetailsViewController.h"
@interface MainMHViewController ()
@property (nonatomic, strong) NSMutableArray *datasourceArr;
@end

@implementation MainMHViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self configureData];
    self.navigationItem.title = @"每日十件美好礼物";
    [self.tableView registerClass:[OtherTableViewCell class] forCellReuseIdentifier:@"OtherTableViewCell"];
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
}
- (void)configureData
{
    NSString *str = [NSString stringWithFormat:@"http://api.liwushuo.com/v2/collections/22/posts?gender=1&generation=1&limit=20&offset=0"];
        _datasourceArr = [NSMutableArray arrayWithCapacity:0];
    
    [PKRequestManager requestWithTypeP:GET urlString:str parDic:nil finish:^(NSData *data) {

        
        NSDictionary *dict =[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
        
        
        for (NSDictionary *dic in dict[@"data"][@"posts"]) {
            OtherTableViewModel *model = [[OtherTableViewModel alloc]init];
            [model setValuesForKeysWithDictionary:dic];
            [_datasourceArr addObject:model];
            
        }
        [self.tableView reloadData];
    } error:^(NSError *error) {
        
    }];
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 200;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
#warning Incomplete implementation, return the number of sections
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
#warning Incomplete implementation, return the number of rows
    return _datasourceArr.count;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    StrategyDetailsViewController *strategDetailsVC = [[StrategyDetailsViewController alloc]init];
    strategDetailsVC.contentId = ((OtherTableViewModel *)_datasourceArr[indexPath.row]).Myid;
    strategDetailsVC.hidesBottomBarWhenPushed = YES;
    [self.navigationC pushViewController:strategDetailsVC animated:YES];
    strategDetailsVC.hidesBottomBarWhenPushed = NO;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    OtherTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"OtherTableViewCell" forIndexPath:indexPath];
    cell.selectionStyle = NO;
    cell.layer.cornerRadius = 20;
    cell.layer.masksToBounds = YES;
    //cell.textLabel.text = @"dsf";
    
    cell.configure = _datasourceArr[indexPath.row];
    // Configure the cell...
    
    return cell;
}

/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
